const ioredis = require('ioredis')
const fs = require('fs')
// 1. Create connection
// redis-cli
// config set requirepass 'xxxxxx'
const redis = new ioredis({
    port: 6379,
    host: '42.192.138.208',
    password: '123456'
})

exports.pick = (key, callback) => {
    redis.hgetall(key).then(res => {
        console.log(key, res);
        callback({
            code: 1,
            msg: res
        });
    }).catch(err => {
        callback({
            code: 0,
            msg: err
        });
    })
}

const types = ['male', 'female', 'all'];
function storeMap(key, obj) {
    redis.hmset(key, obj).then(res => {
        console.log("success", res);
    }).catch(err => {
        console.log(err);
    })
}
function getRandom(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function throwAllBottles(num) {
    for (let i = 0; i < num; i++) {
        let obj = {
            owner: `Drift${i}`,
            type: types[getRandom(0, types.length)],
            content: `This is drift ${i} !`,
            time: new Date()
        }
        storeMap(`Drift-${i}`, obj)
    }
}

// https://gitee.com/mirrors/ioredis